//
//  LayerHugeCountViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/11/4.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "LayerHugeCountViewController.h"

#define WIDTH 10
#define HEIGHT 10
#define DEPTH 10
#define SIZE 100
#define SPACING 150
#define CAMERA_DISTANCE 500

@interface LayerHugeCountViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *clickArray;

@end

@implementation LayerHugeCountViewController

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.contentSize = CGSizeMake((WIDTH - 1) * SPACING, (HEIGHT - 1) * SPACING);
    CATransform3D transform3D = CATransform3DIdentity;
    transform3D.m34 = - 1.0 / CAMERA_DISTANCE;
    self.scrollView.layer.sublayerTransform = transform3D;
    [self.view addSubview:_scrollView];
    NSMutableArray *visibles = [NSMutableArray array];
    // 创建3D控件的图层
    for (int z = (DEPTH - 1); z >= 0; z--) {
        for (int y = 0; y < HEIGHT; y++) {
            for (int x = 0; x < WIDTH; x++) {
                CALayer *layer = [CALayer layer];
                layer.frame = CGRectMake(0, 0, SIZE, SIZE);
                layer.position = CGPointMake(x * SPACING, y * SPACING);
                layer.zPosition = - z * SPACING;
                layer.backgroundColor = [UIColor colorWithWhite:1-z*(1.0 / DEPTH) alpha:1].CGColor;
             //   [self.scrollView.layer addSublayer:layer];
                [visibles addObject:layer];
            }
        }
    }
    self.clickArray = [NSMutableArray arrayWithArray:visibles];
    self.scrollView.layer.sublayers = visibles;
    NSLog(@"子图层的数量:%i", DEPTH * HEIGHT * WIDTH);
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    returnButton.backgroundColor = [UIColor cyanColor];
    returnButton.frame = CGRectMake(0, 0, 30, 30);
    [returnButton addTarget:self action:@selector(returnBack:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:returnButton];
}

- (void)returnBack:(UIButton *)button
{
   
   [self dismissViewControllerAnimated:YES completion:nil];
}





@end
